package com.cjq.allservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjq.allservice.entity.Cart;
import com.cjq.allservice.entity.OrderDetail;
import com.cjq.allservice.entity.Orders;
import com.cjq.allservice.entity.vo.CartVo;
import com.cjq.allservice.service.CartService;
import com.cjq.allservice.service.OrderDetailService;
import com.cjq.allservice.service.OrdersService;
import com.cjq.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjq
 * @since 2022-01-11
 */
@RestController
@RequestMapping("/allservice/cart")
@CrossOrigin
public class CartController {

    @Autowired
    private CartService cartService;

    private final OrdersService ordersService;
    private final OrderDetailService orderDetailService;


    public CartController(OrdersService ordersService,OrderDetailService orderDetailService){
        this.ordersService=ordersService;
        this.orderDetailService=orderDetailService;
    }



    @ApiOperation("添加商品进入购物车")
    @PostMapping("/addCart")
    public R addCart(@RequestBody Cart cart){
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("cart_goods_id",cart.getCartGoodsId());
        Cart cart1 = cartService.getOne(wrapper);
        if (cart1!=null){

            cart1.setCartGoodsNumber(cart1.getCartGoodsNumber()+1);
            boolean b = cartService.updateById(cart1);
            if (b){
                return R.succ().message("添加购物车成功！");
            }else{
                return R.fail().message("添加添加购物车失败！");
            }


        }else {
            boolean save = cartService.save(cart);
            if (save==true){
                return R.succ().message("添加购物车成功！");
            }else{
                return R.fail().message("添加添加购物车失败！");
            }
        }


    }




    @ApiOperation("根据用户id获取购物车列表")
    @GetMapping("/getCartByUserId")
    public R getCartByUserId(@RequestParam String userId){
        List<CartVo> carts = cartService.getAllCartsByUserId(userId);
        return R.succ().data("cartList",carts);
    }




    @ApiOperation("删除购物车内容")
    @DeleteMapping("/deleteCartById")
    public R deleteCartById(@RequestParam String cartId){
        boolean b = cartService.removeById(cartId);
        if (b==true){
            return R.succ().message("删除成功！");
        }else {
            return R.fail().message("删除失败");
        }
    }


    @ApiOperation("修改前台复选框的勾选状态")
    @PostMapping("/changeCheckedByCartId")
    public R changeCheckedByCartId(@RequestParam String cartId){
        Cart cart = cartService.getById(cartId);
        if (cart.getChecked()==0){
            cart.setChecked(1);
        }else {
            cart.setChecked(0);
        }
        boolean b = cartService.updateById(cart);
        if (b==true){
            return R.succ().message("修改成功！");
        }else{
            return R.fail().message("修改失败！");
        }
    }


    @ApiOperation("全选功能实现")
    @PostMapping("/changeAllChecked")
    public R changeAllChecked(@RequestParam String userId,@RequestParam int allCheck){
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("cart_user_id",userId);
        List<Cart> list = cartService.list(wrapper);
        if (allCheck==1){
            for (int i = 0; i <list.size() ; i++) {
                list.get(i).setChecked(1);
            }
        }else if(allCheck==0) {
            for (int i = 0; i <list.size() ; i++) {
                list.get(i).setChecked(0);
            }
        }
        for (int i = 0; i <list.size() ; i++) {
            cartService.updateById(list.get(i));
        }

        return R.succ().message("全选功能成功实现！");
    }

    @ApiOperation("进行购物车数量的修改")
    @PostMapping("/editCartNumber")
    public R editCartNumber(@RequestParam String cartId,@RequestParam Integer operation){
        Cart cart = cartService.getById(cartId);
        Integer number = cart.getCartGoodsNumber();
        if (number!=0){
            cart.setCartGoodsNumber(number+operation);
        }

        boolean b = cartService.updateById(cart);
        if (b){
            return R.succ().message("修改数量成功！");
        }else {
            return R.fail().message("修改失败！");
        }

    }


    @ApiOperation("从购物车生成订单")
    @PostMapping("/clearCheckedCartAndCreateOrder")
    public R clearCheckedCartAndCreateOrder(@RequestParam String userId,@RequestParam Integer number,@RequestParam Double price,@RequestParam String addressId){
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("cart_user_id",userId);
        wrapper.eq("checked",1);
        List<Cart> list = cartService.list(wrapper);
        Orders orders = new Orders();
        orders.setOrderUserId(userId);
        orders.setOrderTotalPrice(price);
        orders.setOrderGoodsNumber(number);
        orders.setOrderAddressId(addressId);
        ordersService.save(orders);
        for (Cart cart:list) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orders.getOrderId());
            orderDetail.setGoodsId(cart.getCartGoodsId());
            orderDetail.setNumber(cart.getCartGoodsNumber());
            orderDetail.setUserId(userId);
            orderDetailService.save(orderDetail);
            cartService.removeById(cart.getCartId());
        }

        return R.succ().data("ordersId",orders.getOrderId()).message("成功从购物车生成订单！");
    }

}

